package TEXT6;

import java.util.*;

public class Main {
    static int N = 200010, n, m;
    static Edgs[] edgs = new Edgs[N];
    static int[] p = new int[N];//并查集集合

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        for (int i = 1; i <= n; i++) {
            p[i] = i;
        }
        for (int i = 0; i < m; i++) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            int c = scanner.nextInt();
            edgs[i]=new Edgs(a,b,c);
        }
        kruskal();
    }

    private static void kruskal() {
        int res = 0;//所有权重之和
        int cnt = 0;//加入集合的边数之和
        Arrays.sort(edgs,0,m);
        for (int i = 0; i < m; i++) {
            int a=edgs[i].a;
            int b=edgs[i].b;
            int c=edgs[i].c;
            a=find(a);b=find(b);
            if(a!=b){
                p[a]=b;
                cnt++;
                res+=c;
            }
        }
        if(cnt<n-1){
            System.out.println("impossible");
        }else {
            System.out.println(res);
        }
    }

    private static int find(int x) {
        if(p[x]!=x){
            p[x]=find(p[x]);
        }
        return p[x];
    }
}

class Edgs implements Comparable<Edgs> {
    int a, b, c;

    public Edgs(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public int compareTo(Edgs o) {
        return Integer.compare(c, o.c);
    }
}